<?php
class XemtinthongbaosController extends AppController {

	var $name = 'Xemtinthongbaos';
	var $uses = array('Xemtinthongbao','Tinthongbao','Chucv');
	
	function index() {
		$this->Xemtinthongbao->recursive = 0;
		$this->set('xemtinthongbaos', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid xemtinthongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('xemtinthongbao', $this->Xemtinthongbao->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Xemtinthongbao->create();
			if ($this->Xemtinthongbao->save($this->data)) {
				$this->Session->setFlash(__('The xemtinthongbao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The xemtinthongbao could not be saved. Please, try again.', true));
			}
		}
		$tinthongbaos = $this->Xemtinthongbao->Tinthongbao->find('list');
		$chucvs = $this->Xemtinthongbao->Chucv->find('list');
		$this->set(compact('tinthongbaos', 'chucvs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid xemtinthongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Xemtinthongbao->save($this->data)) {
				$this->Session->setFlash(__('The xemtinthongbao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The xemtinthongbao could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Xemtinthongbao->read(null, $id);
		}
		$tinthongbaos = $this->Xemtinthongbao->Tinthongbao->find('list');
		$chucvs = $this->Xemtinthongbao->Chucv->find('list');
		$this->set(compact('tinthongbaos', 'chucvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for xemtinthongbao', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Xemtinthongbao->delete($id)) {
			$this->Session->setFlash(__('Xemtinthongbao deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Xemtinthongbao was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function themthongbao()
	{
		if(!empty($this->data))
		{
			$record['Tinthongbao']['tieude']=$this->data['Xemtinthongbao']['tieude'];
			$record['Tinthongbao']['noidung']=$this->data['Xemtinthongbao']['noidung'];
			$record['Tinthongbao']['z1user_id']=$this->Session->read('userid');
			$record['Tinthongbao']['khoa_id']=$this->Session->read('khoa_id');
			$record['Tinthongbao']['active']=$this->data['Xemtinthongbao']['hienthi'];
			$err=0;
			if($this->Tinthongbao->saveAll($record))
			{
				$chucvus =$this->data['Xemtinthongbao']['chucv_ids'];
				$xemtt['Xemtinthongbao']['tinthongbao_id']=$this->Tinthongbao->id;
				foreach($chucvus as $cv)
				{
					$xemtt['Xemtinthongbao']['chucv_id']=$cv;
					if(!$this->Xemtinthongbao->saveAll($xemtt))
						$err=1;
				}
			}
			else
				$err=1;
			
			if($err==1)
			{
				$this->Session->setFlash(__('Lỗi khi lưu', true),'error');
				$this->redirect(array('controller'=>'tinthongbaos','action'=>'listthongbao'));
			}
			else
			{
				$this->Session->setFlash(__('Đăng thông báo thành công.', true),'success');
				$this->redirect(array('controller'=>'tinthongbaos','action'=>'listthongbao'));
			}
		}
		$this->Chucv->displayField = 'tenchucvu';
		$chucvs = $this->Chucv->find('list');
		$this->set(compact('chucvs'));
	}

	function suathongbao($id = null)
	{
		if (!$id)
		{
			$this->Session->setFlash(__('Thông báo không tồn tại', true),'error');
			//$this->redirect(array('action'=>'them'));
		}
		if(!empty($this->data))
		{
			$record['Tinthongbao']['id']=$this->data['Xemtinthongbao']['id'];
			$record['Tinthongbao']['tieude']=$this->data['Xemtinthongbao']['tieude'];
			$record['Tinthongbao']['noidung']=$this->data['Xemtinthongbao']['noidung'];
			$record['Tinthongbao']['z1user_id']=$this->Session->read('userid');
			$record['Tinthongbao']['khoa_id']=$this->Session->read('khoa_id');
			$record['Tinthongbao']['active']=$this->data['Xemtinthongbao']['hienthi'];
			$err=0;
			if($this->Tinthongbao->saveAll($record))
			{
				$chucvus =$this->data['Xemtinthongbao']['chucv_ids'];
				$this->Xemtinthongbao->deleteAll(array('tinthongbao_id'=>$this->data['Xemtinthongbao']['id']));
				$xemtt['Xemtinthongbao']['tinthongbao_id']=$this->Tinthongbao->id;
				foreach($chucvus as $cv)
				{
					$xemtt['Xemtinthongbao']['chucv_id']=$cv;
					if(!$this->Xemtinthongbao->saveAll($xemtt))
						$err=1;
				}
			}
			else
				$err=1;
			
			if($err==1)
			{
				$this->Session->setFlash(__('Lỗi khi lưu', true),'error');
				$this->redirect(array('controller'=>'tinthongbaos','action'=>'listthongbao'));
			}
			else
			{
				$this->Session->setFlash(__('Đăng thông báo thành công.', true),'success');
				$this->redirect(array('controller'=>'tinthongbaos','action'=>'listthongbao'));
			}
		}
		$tintb = $this->Tinthongbao->read(null,$id);
		$this->Xemtinthongbao->displayField='chucv_id';
		$cvs = $this->Xemtinthongbao->find('list',array('conditions'=>array('tinthongbao_id'=>$tintb['Tinthongbao']['id'])));
		$this->Chucv->displayField = 'tenchucvu';
		$chucvs = $this->Chucv->find('list');
		$this->set(compact('chucvs','cvs','tintb','id'));
		
	}
	
	function xemthongbao($id = null)
	{
		if (!$id) {
			$this->Session->setFlash(__('Invalid xemtinthongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('xemtinthongbao', $this->Xemtinthongbao->read(null, $id));
	}
}
